#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QtDeclarative>

#include <mycallback.h>
#include <nativeaudioplayer.h>

#include <hardware/emocap/sbs2emocapdatareader.h>
#include <hardware/emotiv/sbs2emotivdatareader.h>

Q_DECL_EXPORT int main(int argc, char *argv[])
{
    QScopedPointer<QApplication> app(createApplication(argc, argv));

    qDebug() << "catalogPath: "<<Sbs2Common::setDefaultCatalogPath();
    qDebug() << "rootAppPath: "<<Sbs2Common::setDefaultRootAppPath();

    NativeAudioPlayer* audioPlayer = new NativeAudioPlayer();
    //audioPlayer->play();

    MyCallback* myCallback = new MyCallback();
    myCallback->audioPlayer = audioPlayer;
    Sbs2EmotivDataReader* sbs2DataReader = Sbs2EmotivDataReader::New(myCallback,0);

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/AudioTest/main.qml"));
    viewer.showExpanded();

    QObject *rootObject = dynamic_cast<QObject*>(viewer.rootObject());



    //required for proper closing of the app
    QObject::connect(app.data(), SIGNAL(aboutToQuit()), sbs2DataReader, SLOT(aboutToQuit()));
    QObject::connect(app.data(), SIGNAL(aboutToQuit()),myCallback,SLOT(save()));
    QObject::connect((QObject*)viewer.engine(), SIGNAL(quit()), app.data(), SLOT(quit()));
    QObject::connect(audioPlayer,SIGNAL(event(QString)),myCallback,SLOT(insertIntoMetaFile(QString)));
    QObject::connect(rootObject,SIGNAL(startRecording(QString, QString)),myCallback,SLOT(startRecording(QString,QString)));
    QObject::connect(rootObject,SIGNAL(startPlayback1()),myCallback,SLOT(startPlayback1()));
    QObject::connect(rootObject,SIGNAL(startPlayback2()),myCallback,SLOT(startPlayback2()));

    QObject::connect(myCallback,SIGNAL(playSingle()),audioPlayer,SLOT(playSingle()));

    return app->exec();
}
